// pages/duty/duty.js
var app = getApp()
var util = require('../../utils/util.js')  
Page({

  /**
   * 页面的初始数据
   */
  data: {
    hiddenmodalput: true, 
    dutyContent:'',
    feeds:[],
    feeds1:[],
    feeds2:[],
    page:1,
    page1:1,
    page2:1,
    page3:1,
    abcc:'',
    showModal:false,
    date:''
  },
  onReachBottom:function(){
    wx.showLoading({
      title: '加载更多内容',
    })
    var that=this;
      this.getFeeds1(that.data.page1);
      this.getFeeds(that.data.page);
  },

  // /**
  //  * 生命周期函数--监听页面加载
  //  */
  onLoad: function () {
    var that = this;
    var date = util.formatTime(new Date());
    var date1=date.substring(0,4);
    console.log(date1);
    that.setData({
      date:date1
    })
    this.getFeeds1(that.data.page1);
    this.getFeeds(that.data.page);
  },

  getFeeds:function(page){
    if(page==1){
      wx.showLoading({
        title: '加载中',
      })
    }
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid") //读取cookie
    };
    var that=this
    wx.request({
      url: app.globalData.baseUrl +'/safe/sysDutyType/find?pageCurrent	=' +
        page + '&pageSize=10',
        method:'post',
        header:header,
        success:function(res){
          var res=res.data;
          console.log(res);
          if(that.data.page>1){
            var feedTemp=that.data.feeds;
            that.setData({
              feeds: Object.assign(feedTemp,res.data),
              page:page + 1
            })
          }else{
            that.setData({
              feeds:res.data,
              page:page+1
            })
          }
          wx.hideLoading();
        }
    })
  },

  getFeeds1: function (page) {
    var date = new Date();
    var myDate = date.getFullYear();
    var user = app.globalData.user
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid") //读取cookie
    };
    if (page == 1) {
      wx.showLoading({
        title: '加载中',
      })
    }
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/safe/sysDuty/find?pageCurrent	=' +
        page + '&userId=' + user.userId + '&year=' + myDate + '&pageSize=10',
      header: header,
      method: 'post',
      success: function (res) {
        var res = res.data;
        console.log(res.data.list);
        for (var item in res.data.list){
          var dateObj = new Date();
          dateObj.setTime(res.data.list[item].dutyDate);
          res.data.list[item].dutyDate = util.formatTime(dateObj);
        }
        if (that.data.page1 > 1) {
          var feedTemp = that.data.feeds1;
          that.setData({
            feeds1: Object.assign(feedTemp,res.data.list),
            page1: page + 1
          })

        } else {
          that.setData({
            feeds1: res.data.list,
            page1: page + 1
          })
        }
        wx.hideLoading()
      }
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  },
  showButton: function () {
    var that = this;
    if (that.data.showView1 == false) {
      that.setData({
        showView1: (!that.data.showView1),
        showView: (!that.data.showView)
      })
    }
  },
  showButton1: function () {
    var that = this;
    if(that.data.showView1==true){
      that.setData({
        showView: (!that.data.showView),
        showView1: (!that.data.showView1),
      })
    }

  },
  bindDateChange: function (e) {
    var that=this;
    console.log('picker发送选择改变，携带值为', e.detail.value);
    var date11=e.detail.value.substring(0,4);
    console.log(date11);
    this.setData({
      date: date11
    })
    this.getFeeds(that.data.page);
  },
  holidayDetail:function(e){
    var user = app.globalData.user
    var header;
    var abc = e.currentTarget.dataset.abc;
    var bcd = e.currentTarget.dataset.bcd;
    var companyId = user.parentCompanyId;
    if(companyId==null){
      companyId=1;
    }
    wx.navigateTo({
      url: '/pages/duty/dutydetail/dutydetail?companyId=' + companyId + '&dutyTypeCode=' + abc + '&year=' + bcd,
    })
  },

  select:function(e){
    console.log(e.currentTarget.dataset.name);
  },

  modalinput: function (e) {
    var that=this;
    var abc1 = e.currentTarget.dataset.abc1;
    this.setData({
      hiddenmodalput: !that.data.hiddenmodalput,
      showModal: true,
      abcc:abc1
    })
    console.log(that.data.abcc);
    console.log(that.data.hiddenmodalput);
  },
  //取消按钮  
  cancel: function () {
    this.setData({
      hiddenmodalput: true
    });
  },
  //确认  
  confirm: function (e) {
    var user = app.globalData.user
    var header;
    header = {
      'Cookie': wx.getStorageSync("sessionid") //读取cookie
    };
    // if (page == 1) {
    //   wx.showLoading({
    //     title: '加载中',
    //   })
    // }
    var that = this
    wx.request({
      url: app.globalData.baseUrl + '/safe/sysDuty/update?id	=' +
        that.data.abcc + '&dutyContent=' + that.data.dutyContent,
      header: header,
      method: 'post',
      success: function (res) {
        var res = res.data;
        console.log(res);

      }
    })
    this.setData({
      hiddenmodalput: true
    })
    this.getFeeds1(that.data.page1);
  },
  jiluInput: function (e) {
    this.setData({
      dutyContent: e.detail.value
    })
    // console.log(this.data.dutyContent);
  },

  

})